/* sun_NetMapUI.js
 * NET地图交互类插件新增的ui类
*/

//屏蔽掉主菜单按键
Scene_Map.prototype.isMenuEnabled = function () {
    return false;
}

//将地图名字窗口改为tip窗口
Scene_Map.prototype.createMapNameWindow = function () {
    const rect = this.mapNameWindowRect();
    this._mapNameWindow = new Window_Tip(rect);
    this.addWindow(this._mapNameWindow);
};

Scene_Map.prototype.mapNameWindowRect = function () {
    const wx = 0;
    const wy = 150;
    const ww = Graphics.boxWidth;
    const wh = this.calcWindowHeight(1, false);
    return new Rectangle(wx, wy, ww, wh);
}

Scene_Map.prototype.onTransferEnd = function () {
    if ($gameMap.displayName()) {
        this._mapNameWindowpop($gameMap.displayName());
    }
    $gameMap.autoplay();
    if (this.shouldAutosave()) {
        this.requestAutosave();
    }
}

//===============创建新增窗口===============



//玩家信息窗口：该窗口为地图场景中显示于右上角的你自己的信息的窗口
//包括头像、名字、登记、职业、血条、蓝条
Scene_Map.prototype.createMyInfoWindows = function () {
    const rect = this.myInfoWindowsRact();
    this._myInfoWindow = new Window_MyInfo(rect);
    this.addWindow(this._myInfoWindow);
}

Scene_Map.prototype.myInfoWindowsRact = function () {
    const ww = 300;
    const wh = 150;
    const wx = Graphics.boxWidth - ww;
    const wy = 0;
    return new Rectangle(wx, wy, ww, wh);
}

//目标信息窗口：该窗口为地图场景点击其他玩家后显示选中玩家的信息的窗口
//包括头像、名字、等级、职业、血条、蓝条
//点击该窗口可以触发交互菜单，组队、好友、交易等交互选项
Scene_Map.prototype.createTargetWindows = function () {
    const rect = this.targetWindowsRact();
    this._targetWindow = new Window_Target(rect);
    this._targetWindow.hide();
    this.addWindow(this._targetWindow);
}

Scene_Map.prototype.targetWindowsRact = function () {
    const ww = 250;
    const wh = 85;
    const wx = (Graphics.boxWidth - ww) / 2;
    const wy = 0;
    return new Rectangle(wx, wy, ww, wh);
}

//玩家选项窗口：该窗口为点击目标信息窗口后唤出的与其他玩家交互的窗口
//目前只有组队功能
Scene_Map.prototype.createPlayerCommandWindows = function () {
    const rect = this.playerCommandWindowsRact();
    this._playerCommandWindow = new Window_PlayerCommand(rect);
    this._playerCommandWindow.setHandler("party", NetMap.commandParty);
    this._playerCommandWindow.hide();
    this._playerCommandWindow.deactivate();
    this.addWindow(this._playerCommandWindow);
}

Scene_Map.prototype.playerCommandWindowsRact = function () {
    const ww = this.mainCommandWidth();
    const wh = this.calcWindowHeight(1, true);
    const wx = (Graphics.boxWidth - ww) / 2;
    const wy = 170;
    return new Rectangle(wx, wy, ww, wh);
}

//玩家选项窗口,选择组队命令时触发的函数
// Scene_Map.prototype.commandParty = function () {
//     if (NetManager.party) {
//         const num = Object.keys(NetManager.party).length;
//         if (num > 5) {
//             this._mapNameWindow.pop("队伍已满！");
//         }
//     }
//     const playerName = this._playerCommandWindow._player;
//     NetMap.partyInvite(playerName);
//     this._playerCommandWindow.hide();
// }

//交互确认窗口：该窗口为处理收到其他玩家交互请求的确认窗口
//用于给玩家同意或拒绝该请求
Scene_Map.prototype.createConfirmWindows = function () {
    const rect = this.confirmWindowsRact();
    this._confirmWindow = new Window_PlayerConfirm(rect);
    this._confirmWindow.hide();
    this.addWindow(this._confirmWindow);
}

Scene_Map.prototype.confirmWindowsRact = function () {
    const ww = 500;
    const wh = 200;
    const wx = (Graphics.boxWidth - ww) / 2;
    const wy = (Graphics.boxHeight - wh) / 2;
    return new Rectangle(wx, wy, ww, wh);
}

//队伍成员窗口：该窗口为显示队伍中成员列表的窗口
//还未决定点击该窗口直接进入交互场景还是直接唤出对每个玩家操作的选项窗口
Scene_Map.prototype.createPartyMemberWindows = function () {
    const rect = this.partyWindowsRact();
    this._partyWindow = new Window_PartyMember(rect);
    this.addWindow(this._partyWindow);
}

Scene_Map.prototype.partyWindowsRact = function () {
    const ww = 220;
    const wh = 300;
    const wx = Graphics.boxWidth - ww - 10;
    const wy = (Graphics.boxHeight - wh) / 2;
    return new Rectangle(wx, wy, ww, wh);
}

//===============创建新增窗口结束===============



//===============玩家信息窗口===============
class Window_MyInfo extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this.setActor();
        this.setBackgroundType(3);
        this.createsSprite();
        this.refresh();
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#FFDEAD";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h / 2 + 10, c, 10);
            bitmap.fillRoundRect(w / 2, 0, w / 2, h, c, 10);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }

    setActor() {
        this._actor = NetManager.netActor();
    }

    refresh() {
        this.contents.clear();
        this.drawName();
        this.drawFace();
    }

    // set(faceName, faceIndex, name) {
    //     this._faceName = faceName;
    //     this._faceIndex = faceIndex;
    //     this._name = name;
    // }

    createsSprite() {
        const x = 15;
        const y = 38;
        this.createGaugeSprite(x, y);
        this.createFaceSprite(this.width - this.height + 5, 5);
    }

    createFaceSprite(x, y) {
        const w = this.height - 10;
        const h = this.height - 10;
        const c = "#A0522D";
        this._faceBitmap = new Bitmap(w, h * 2);
        this._faceBitmap.fillRoundRect(0, 0, w, h, c, 5);
        this._faceSprite = new Sprite_NetButton(x, y, this._faceBitmap, this.onClick.bind(this));
        this.addChild(this._faceSprite);
    }

    drawName() {
        const x = 6;
        const y = -8;
        const w = 120;
        const name = this._actor.name();
        const lv = this._actor.level;
        // const className = $dataClasses[this._actor._classId].name;
        const text = `${name}  lv${lv}`;
        this.contents.fontSize = 20;
        this.drawText(text, x, y, w, "left");
    }

    drawFace() {
        if (!this._actor) return;
        const w = this._faceBitmap.width - 10;
        const h = this._faceBitmap.height / 2 - 10;
        const x = 5;
        const y = 5;
        const faceName = this._actor._faceName;
        const faceIndex = this._actor._faceIndex;
        const bitmap = ImageManager.loadFace(faceName);
        bitmap.addLoadListener(() => {
            const pw = ImageManager.faceWidth;
            const ph = ImageManager.faceHeight;
            const sx = (faceIndex % 4) * pw;
            const sy = Math.floor(faceIndex / 4) * ph;
            this._faceBitmap.blt(bitmap, sx, sy, pw, ph, x, y, w, h);
        })
    }

    createGaugeSprite(x, y) {
        this._hpGauge = new Sprite_MapGauge();
        this._mpGauge = new Sprite_MapGauge();
        this._hpGauge.set(this._actor, "hp");
        this._mpGauge.set(this._actor, "mp");
        this._hpGauge.move(x, y);
        this._mpGauge.move(x, y + 20);
        this.addChild(this._hpGauge);
        this.addChild(this._mpGauge);
    }

    onClick() {
        SceneManager.push(Scene_ActorState);
    }
}

//===============目标信息窗口===============
class Window_Target extends Window_MyInfo {
    initialize(rect) {
        super.initialize(rect);
        this._actor = {};
    }

    setActor(player) {
        if (player) {
            const actor = this._actor;
            const { name, level, characterName, characterIndex, faceName, faceIndex, classId } = player._data;
            actor._name = name;
            actor._level = level;
            actor._characterName = characterName;
            actor._characterIndex = characterIndex;
            actor._faceName = faceName;
            actor._faceIndex = faceIndex;
            actor._classId = classId;
        }
    }

    refresh() {
        this.contents.clear();
        this.drawFace();
        this.drawName();
    }

    createsSprite() {
        const x = 90;
        const y = 40;
        this.createGaugeSprite(x, y);
        this.createFaceSprite(5, 5);
    }

    drawName() {
        if (!this._actor) return;
        const x = 90;
        const y = -8;
        const w = this.width - 120;
        const name = this._actor._name;
        const lv = this._actor._level;
        // const className = $dataClasses[this._actor._classId].name;
        const text = `${name}  lv${lv}`;
        this.contents.fontSize = 20;
        this.drawText(text, x, y, w, "left");
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#FFDEAD";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h, c, 10);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }
}

//===============玩家选项窗口===============
class Window_PlayerCommand extends Window_Command {
    initialize(rect) {
        super.initialize(rect);
        this._player = "";
    }

    setPlayer(player) {
        this._player = player;
    }

    makeCommandList() {
        this.addCommand("组队", "party", true);
    }
}

//===============交互确认窗口===============
class Window_PlayerConfirm extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this._text = "";
        this._mode = "";
        this.createButton();
        this.refresh();
    }

    createButton() {
        this.createAgreeButton();
        this.createRefuseButton();
    }

    refresh() {
        this.contents.clear();
        // this.contents.fontSize = 35;
        this.drawText(this._text, 0, 5, this.width - 20, "left");
    }

    setText(text) {
        this._text = text;
    }

    setMode(mode) {
        this._mode = mode;
    }

    createAgreeButton() {
        const text = "同意";
        const fun = this._agree.bind(this);
        const button = new Sprite_NetButton(30, 120, text, fun);
        this.addChild(button);
        this._logonButton = button;
    }

    createRefuseButton() {
        const text = "拒绝";
        const fun = this._refuse.bind(this);
        const button = new Sprite_NetButton(270, 120, text, fun);
        this.addChild(button);
        this._logonButton = button;
    }

    _agree() {
        NetMap.partyAgree();
        this.hide();
    }

    _refuse() {
        NetMap.partyRefuse();
        this.hide();
    }

    show() {
        this.refresh();
        super.show();
    }

    hide() {
        this._text = "";
        this._mode = "";
        super.hide();
    }
}

//===============队伍成员窗口===============
class Window_PartyMember extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this.setBackgroundType(3);
        this._member = null;
        this.createButton();
        this.refresh();
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#171717";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h, c, 10);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }

    createButton() {
        const x = 10;
        const y = this.height - 70;
        const text = "离开队伍";
        const button = new Sprite_NetButton(x, y, text, NetMap.leaveParty);
        this.addChild(button);
        this._leaveButton = button;
    }

    refresh() {
        this.contents.clear();
        this._data = NetManager.party;
        this.drawMember();
    }

    drawMember() {
        if (!this._data) return;
        const member = Object.keys(this._data);

        const x = 5;
        let y = 0;
        for (const player of member) {
            if (!this._data[player]) {
                this.drawText(player, x, y, this.width, "left");
                this.drawText("离线", x, y, this.width, "right");
                y += 35;
                continue;
            }
            const index = 320 + this._data[player].classId;
            this.drawIcon(index, x, y)
            this.drawText(player, x + 35, y, this.width - 35, "left");
            y += 35;
        }
    }
}

//===============地图血条精灵===============
class Sprite_MapGauge extends Sprite {
    initialize() {
        super.initialize();
        this.initMembers();
        // this.loadBitmap();
        this.createBitmap();
    }

    initMembers() {
        this._actor = null;
        this._type = "";
        this._maxValue = NaN;
        this._value = NaN;
    }

    createBitmap() {
        const bitmap = new Bitmap(120, 16);
        this.bitmap = bitmap;
    }

    refresh() {
        this.bitmap.clear();
        this.drawValue();
    }

    drawValue() {
        if (!this._actor) return;
        const bitmap = ImageManager.loadNet("gauge_baseA");
        const valueName = this._type === "hp" ? "gauge_bar_orange" : "gauge_bar_blue";
        const valueBitmap = ImageManager.loadNet(valueName);
        bitmap.addLoadListener(() => {
            this.bitmap.blt(bitmap, 0, 0, 120, 16, 0, 0);
        })
        const rate = this._value / this._maxValue;
        const w = 120 * rate;
        valueBitmap.addLoadListener(() => {
            this.bitmap.blt(valueBitmap, 0, 0, 120, 16, 0, 0, w, 16);
        })
    }

    update() {
        if (!this._actor) return;
        super.update();
        const value = this._type === "hp" ? this._actor.hp : this._actor.mp;
        const maxValue = this._type === "hp" ? this._actor.mhp : this._actor.mmp;
        if (this._value != value || this._maxValue != maxValue) {
            this.updateValue();
            this.refresh();
        }
    }

    updateValue() {
        if (!this._actor) return;
        this._value = this._type === "hp" ? this._actor.hp : this._actor.mp;
        this._maxValue = this._type === "hp" ? this._actor.mhp : this._actor.mmp;
    }

    set(actor, type) {
        if (!actor) return;
        this._actor = actor;
        this._type = type;
        this.updateValue();
        this.refresh();
    }
}

//===============玩家列表窗口================
//该窗口没有在场景加载时创建
class Window_PlayList extends Window_Selectable {
    initialize(list, x, y) {
        const numLines = list.length;
        const rect = this.windowRect(numLines, x, y);
        Window_Selectable.prototype.initialize.call(this, rect);
        this._data = list;
        this._data.push(null);
        this.setHandler("ok", this.onOk.bind(this));
        this.refresh();
        this.activate();
    }

    windowRect(numLines, x, y) {
        const wh = this.fittingHeight(numLines + 1);
        const ww = 150;
        const wx = x + ww + 50 > Graphics.boxWidth ? x - 150 : x + 50;
        const wy = y + wh > Graphics.boxHeight ? Graphics.boxHeight - wh - 20 : y - 20;
        return new Rectangle(wx, wy, ww, wh);
    }

    fittingHeight(numLines) {
        return numLines * 44 + $gameSystem.windowPadding() * 2;
    }

    maxCols() {
        return 1;
    }

    maxItems() {
        return this._data ? this._data.length : 1;
    }

    itemAt(index) {
        return this._data && index >= 0 ? this._data[index] : null;
    }

    onOk() {
        const index = this.index();
        const player = this.itemAt(index);
        if (player) {
            SceneManager._scene._targetWindow.setActor(player);
            SceneManager._scene._targetWindow.refresh();
            SceneManager._scene._targetWindow.show();
        }
        this.hide();
        this.deactivate();
    }

    refresh() {
        //this.makeItemList();
        Window_Selectable.prototype.refresh.call(this);
    }

    drawItem(index) {
        const name = this.itemAt(index);
        const rect = this.itemLineRect(index);
        const x = rect.x;
        const y = rect.y;
        const w = rect.width;
        if (name) {
            this.drawText(name, x, y, w, "center");
        } else {
            this.drawText("关闭", x, y, w, "center");
        }
    }

    show(list, x, y) {
        this._data = list;
        const rect = this.windowRect(list.length, x, y);
        this.x = rect.x;
        this.y = rect.y;
        this.width = rect.width;
        this.height = rect.height;
        this._data.push(null);
        this.refresh();
        super.show();
        this.activate();
        NetManager.windowShow = true;
    }
}



